<template>
  <div class="bookshelf">
    <!-- 顶部导航栏 -->
    <div class="header">
      <div class="nav-bar">
        <div class="nav-title">
          <span class="active">书架</span>
        </div>
        <div class="nav-actions">
          <!--浏览历史-->
          <van-icon name="browsing-history-o" size="20" @click="showHistory" />
          <!--编辑书架-->
          <van-icon name="edit" size="20" @click="toggleEdit" />
        </div>
      </div>
    </div>

    <!-- 书籍列表容器 -->
    <div class="book-list">
      <!-- 循环渲染每一本书籍 -->
      <div class="book-item" v-for="(book, index) in books" :key="index" :class="{ 'selected': isEditing && book.selected }" @click="selectBook(book)">
        <!-- 书籍封面展示 -->
        <div class="book-cover">
          <img :src="book.url" :alt="book.name">
          <!-- 如果书籍有阅读进度，则显示阅读进度 -->
          <div class="reading-progress" v-if="book.progress">
            <div class="progress-text">读至{{ book.progress }}%</div>
          </div>
        </div>
        <!-- 书籍标题 -->
        <div class="book-title">{{ book.name }}</div>
        <!-- 复选框 -->
        <van-checkbox v-if="isEditing" v-model="book.selected" class="book-checkbox"></van-checkbox>
      </div>

      <!-- 添加书籍按钮 -->
      <div class="book-item add-book" @click="addBook">
        <div class="add-button">
          <van-icon name="plus" size="24" color="#999" />
          <div class="add-text">去挑书</div>
        </div>
      </div>
    </div>

    <!-- 编辑模式下的操作栏 -->
    <div class="edit-actions" v-if="isEditing">
      <van-button type="default" @click="selectAll">全选</van-button>
      <van-button type="danger" @click="deleteSelectedBooks">删除</van-button>
    </div>

    <!-- 底部导航栏 -->
    <van-tabbar v-model="activeTabbar" route v-if="!isEditing">
      <van-tabbar-item icon="home-o" to="/">首页</van-tabbar-item>
      <van-tabbar-item icon="records" to="/task">任务</van-tabbar-item>
      <van-tabbar-item icon="bookmark-o" to="/bookshelf">书架</van-tabbar-item>
      <van-tabbar-item icon="user-o" to="/mine">我的</van-tabbar-item>
    </van-tabbar>
  </div>
</template>


<script>


export default {
  name: 'BookshelfView',
  data() {
    return {
      activeTabbar: 3,
      books: [],
      user: {},
      uid: 0,
      isEditing: false // 添加编辑模式状态变量
    }
  },
  methods: {
    showHistory() {
      this.$router.push({path:'/history',query:{id:this.uid}})
    },
    toggleEdit() {
      this.isEditing = !this.isEditing
      if (!this.isEditing) {
        this.books.forEach(book => book.selected = false) // 清除选中状态
      }
    },
    // selectBook(book) {
    //   if (this.isEditing) {
    //     book.selected = !book.selected
    //   }
    // },
    selectBook(book) {
      if (this.isEditing) {
        book.selected = !book.selected;
      } else {
        this.$router.push({ path: '/noveldetails', query: { id: book.id } });
      }
    },
    addBook() {
      this.$router.push('/')
    },
    findBooks(uid) {
      this.axios.get('http://localhost:9999/api-novel/novel/findBooks?uid=' + uid).then(res => {
        this.books = res.data.map(book => ({ ...book, selected: false })) // 初始化选中状态
        console.log(res);
      })
    },
    selectAll() {
      const allSelected = this.books.every(book => book.selected);
      this.books.forEach(book => book.selected = !allSelected);
    },
    deleteSelectedBooks() {
      // 通过过滤得到所有选中的书籍
      const selectedBooks = this.books.filter(book => book.selected);
      // 提取选中书籍的ID并放入pid数组
      const pidArray = selectedBooks.map(book => book.id);
      // 将pid数组转换为字符串
      const selectedIds = pidArray.join(',');
      // 输出选中书籍的ID字符串
      console.log('Selected Book IDs:', selectedIds);


      // 移除删除书籍的逻辑
      // this.books = this.books.filter(book => !book.selected);
      this.axios.post(`http://localhost:9999/api-user/user/deleteBooks?id=${this.uid}&ids=${selectedIds}`).then(res => {
        if (res.data.code === 1) {
          this.$toast('删除成功')
          this.findBooks(this.uid);
        }else {
          this.$toast('删除失败')
        }
      })
    }
  },
  created() {
    this.user = localStorage.getItem("user");
    let user = JSON.parse(this.user);
    console.log(user.id)
    this.uid = user.id;
    this.findBooks(this.uid);
  }
}
</script>


<style scoped>
.bookshelf {
  min-height: 100vh;
  background-color: #f7f8fa;
  padding-bottom: 50px;
}

.header {
  background: #fff;
  padding: 10px 16px;
}

.nav-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.nav-title {
  font-size: 16px;
  display: flex;
  gap: 20px;
}

.nav-title span {
  color: #666;
  padding: 4px 0;
}

.nav-title span.active {
  color: #333;
  font-weight: bold;
  position: relative;
}

.nav-title span.active::after {
  content: '';
  position: absolute;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 2px;
  background-color: #00b386;
}

.nav-actions {
  display: flex;
  gap: 16px;
  color: #666;
}

.reading-info {
  display: flex;
  align-items: center;
  gap: 4px;
  background: rgba(0, 179, 134, 0.1);
  color: #00b386;
  font-size: 12px;
  padding: 6px 16px;
}

.book-list {
  padding: 16px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.book-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  position: relative; /* 添加相对定位 */
}

.book-cover {
  position: relative;
  width: 100%;
  margin-bottom: 8px;
}

.book-cover img {
  width: 100%;
  aspect-ratio: 3/4;
  object-fit: cover;
  border-radius: 4px;
}

.reading-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: rgba(0, 0, 0, 0.6);
  padding: 4px;
}

.progress-text {
  color: white;
  font-size: 12px;
  text-align: center;
}

.book-title {
  font-size: 14px;
  color: #333;
  text-align: center;
  width: 100%;
  overflow-wrap: break-word; /* 确保文本换行 */
  word-wrap: break-word; /* 兼容性处理 */
  white-space: normal; /* 允许换行 */
}

.add-book {
  cursor: pointer;
}

.add-button {
  width: 100%;
  aspect-ratio: 3/4;
  border-radius: 4px;
  border: 1px dashed #ddd;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #fff;
}

.add-text {
  margin-top: 8px;
  color: #999;
  font-size: 14px;
}

.book-checkbox {
  position: absolute;
  top: 8px;
  right: 8px; /* 调整复选框的位置 */
}

.edit-actions {
  display: flex;
  justify-content: space-between;
  padding: 10px 16px;
  background: #fff;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  box-shadow: 0 -2px 4px rgba(0, 0, 0, 0.1);
}
</style>



